import { promises } from "dns"

namespace lianxi9_1 {
    interface User {
        id: number,
        username: string,
        email: string,
        isActive: Boolean,
    }

    let userobj: User = {
        id: 1,
        username: '123',
        email: '123',
        isActive: true
    }
    console.log(userobj.username)

    console.log('-------------------------------')
    interface Product {
        name: string,
        price: number,
        description?: string
    }
    const productobj: Product = {
        name: '123',
        price: 124
    }
    for (let key in productobj) {
        console.log(productobj[key as keyof Product])
    }
    console.log('-------------------------------')

    interface MathOperation {
        (a: number, b: number): number
    }

    const MathNum: MathOperation = (a: number, b: number) => {
        return a + b;
    }
    console.log(MathNum(5, 3))
    console.log('-------------------------------')

    interface StringArray { [index: number]: string }
    let strArr1: StringArray = ['123', '321', '234', '432']
    for (const key in strArr1) {
        console.log(strArr1[key])
    }
    console.log('-------------------------------')

    interface Coordinates {
        readonly x: number,
        readonly y: number
    }
    let nate: Coordinates = {
        x: 123,
        y: 321
    }
    console.log(nate.x)
    console.log(nate.y)
    // nate.x = nate.y
    console.log('-------------------------------')

    interface Counter {
        value: number
        (): number
    }
    const counter: Counter = (() => {
        let count = 0;
        const c = () => ++count;
        c.value = 0;
        return c;
    })();
    console.log('-------------------------------')

    interface StringDictionary {
        [key: string]: string
    }
    const dictionary: StringDictionary = {
        hello: 'hello',
        world: 'world'
    }
    console.log(dictionary.hello)
    console.log(dictionary.world)
    console.log('-------------------------------')
    interface Animal {
        name: string,
        age: number
    }
    interface Dog extends Animal {
        breed: string
    }

    const dog: Dog = {
        breed: '321',
        name: '123',
        age: 4556
    }
    for (const key in dog) {
        console.log(dog[key as keyof Dog])
    }
    console.log('-------------------------------')
    interface Person {
        name: string,
        age: number
    }
    interface Employee {
        employeeId: number
    }
    interface Worker extends Person, Employee { }
    const worker: Worker = {
        name: '123',
        age: 123,
        employeeId: 321
    }
    for (const key in worker) {
        console.log(worker[key as keyof Worker])
    }
    console.log('-------------------------------')

    interface Person1 {
        name: string,
        age: number
    }
    interface KeyValuePair<K extends keyof Person1, V extends Person1[K]> {
        key: K
        value: V
    }
    const keyValue: KeyValuePair<'name', string> = {
        key: 'name',
        value: '123'
    }
    console.log(keyValue.key, keyValue.value)
    console.log('-------------------------------')
    type User1 = {
        id: number,
        name: string,
        email: string
    }
    const admin: User1 = {
        id: 1,
        name: 'administator',
        email: 'cnm'
    }
    for (const key in admin) {
        console.log(admin[key as keyof User1])
    }
    console.log('-------------------------------')
    type ApiResponse = {
        status: string,
        data: any
    }
    const response: ApiResponse = {
        status: 'success',
        data: {
            name: 123,
            id: 321
        }
    }
    console.log(response.status, response.data.name, response.data.id)
    console.log('-------------------------------')

    type Product1 = {
        id: number,
        name: string
    }
    type ProductList = Product1[]
    const products: ProductList = [{ id: 1, name: '123' }, { id: 2, name: '234' }]
    console.log(products[0].id, products[0].name, products[1].id, products[1].name)
    console.log('-------------------------------')

    type Status = 'success' | 'error' | 'loading'
    const currentStatus: Status = 'error'
    console.log(currentStatus)

    console.log('-------------------------------')

    type MathOperation1 = (a: number, b: number) => number;
    const add: MathOperation1 = (a: number, b: number) => a + b;
    console.log(add(1, 2))

    console.log('-------------------------------')

    type Person2 = {
        name: string,
        age?: number
    }
    const john: Person2 = {
        name: 'john',
    }
    console.log(john.name, john.age)

    console.log('-------------------------------')

    type Address = {
        street: string,
        city: string
    }
    type Customer = {
        id: number,
        name: string,
        address: Address
    }
    const customer: Customer = {
        id: 1,
        name: '123',
        address: {
            street: '321',
            city: '345'
        }
    }

    console.log('-------------------------------')

    type Animal1 = {
        species: string
    }
    type Mammal = {
        hasFur: boolean
    }
    type Dog1 = Animal1 & Mammal
    const myDog: Dog1 = {
        species: '123',
        hasFur: true
    }
    console.log('-------------------------------')

    type Address1 = (a: number, b: number) => number
    let add1: Address1 = (a, b) => {
        return a + b
    }

    console.log('-------------------------------')

    type Func = (a: number) => string
    const Func1 = (num: number, fn: Func) => {
        console.log(fn(num))
    }
    const func: Func = (a: number) => {
        return `${a}`
    }
    Func1(42, func)
    console.log('-------------------------------')

    type Func2 = (a: number, b: number) => (c: number) => number
    type Func3 = (a: number) => number

    const func2: Func2 = (a, b) => {
        return (c) => a + b + c
    }
    const func3 = func2(1, 2)
    console.log(func3(5))

    console.log('-------------------------------')

    type Func4 = (a: string, b?: boolean) => void
    const func4: Func4 = (a: string, b = false) => { console.log(a, b) }
    func4('123')
    console.log('-------------------------------')

    type Func5 = { a: (b: Array<number>) => number }
    const MathObj = {
        func5: (b: number[]) => {
            const sum = b.reduce((c, d) => c + d, 0);
            return sum / b.length;
        }
    }
    console.log(MathObj.func5([1, 2, 3, 4, 5]))
    console.log('-------------------------------')
    type Func6 = (str: string) => string
    const Func7 = (str: string, func6: Func6): void => {
        console.log(func6(str))
    }
    const func7: Func6 = (str) => {
        return str.toUpperCase();
    }
    Func7('hello', func7)
    console.log('-------------------------------')





}

namespace lianxi9_1_2 {
    class Product {
        name: string
        price: number
        quantity: number
        constructor(name: string, price: number, quantity: number) {
            this.name = name
            this.price = price
            this.quantity = quantity
        }
        get nameer() {
            return this.name
        }
        set nameer(value) {
            this.name = value
        }
        get priced() {
            return this.price
        }
        set priced(value) {
            this.price = value
        }
        get quantited() {
            return this.quantity
        }
        set quantited(value) {
            this.quantity = value
        }
        private sum() {
            return this.price * this.quantity
        }
        getname() {
            console.log(this.name)
            console.log(this.price)
            console.log(this.quantity)
            console.log(this.sum())
        }



    }

}